package com.td.client.states;

import mdes.slick.sui.Button;
import mdes.slick.sui.Display;
import mdes.slick.sui.Label;
import mdes.slick.sui.TextField;
import mdes.slick.sui.ToolTip;
import mdes.slick.sui.Window;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;

import com.td.client.MainGameWindow;
import com.td.client.gui.manager.ResourceManager;
import com.td.core.obj.Player;
import com.td.core.obj.PlayerStats;

public class StatsState extends State{

	private Display display;
	
	public StatsState(MainGameWindow mainGame, GameContainer gameContainer) {
		super(mainGame, gameContainer);
		
		Player[] players = mainGame.getGame().getPlayers();
		PlayerStats[] stats = new PlayerStats[players.length];
		int i = 0;
		for(Player pl : players) {
			
			PlayerStats ps = pl.getStats();
			ps.setBuiltTowers(pl.getTowers().size());
			ps.setPassedUnits(pl.getPassedUnits());
			ps.setPoints(pl.getPoints());
			if(i == mainGame.getPreGameInfo().getMyPlayer().getNumber())
				ps.setName(mainGame.getPreGameInfo().getMyPlayer().getName());
			stats[i++] = ps;
		}
		
		buildGUI(stats);
	}

	@Override
	public void render(GameContainer gc, Graphics g) throws SlickException {
		display.render(gc, g);
		ResourceManager.getImage("stats").draw(100, 300);
	}

	@Override
	public void update(GameContainer gc, int delta) {
		display.update(gc, delta);		
	}
	
	private void buildGUI(PlayerStats... stats) {
		
		display = new Display(gameContainer);
		Window window = new Window();
		window.setOpaque(true);
		window.setBackground(Color.lightGray);
		
		window.setSize(800, 700);
		window.setLocation(0, 0);
		window.setAlwaysOnTop(true);
		
		
		Label players = new Label("player:");
		players.setLocation(50, 310);
		players.setSize(50, 20);
		window.add(players);
		
		Label towers = new Label("towers:");
		towers.setLocation(50, 345);
		towers.setSize(50, 20);
		window.add(towers);
		
		Label bullets = new Label("bullets:");
		bullets.setLocation(50, 380);
		bullets.setSize(50, 20);
		window.add(bullets);
		
		Label passed = new Label("passed:");
		passed.setLocation(50, 415);
		passed.setSize(50, 20);
		window.add(passed);
		
		Label points = new Label("points:");
		points.setLocation(50, 450);
		points.setSize(50, 20);
		window.add(points);
		
		int width = 75;
		int left = 100;
		int i = 0;
		for(PlayerStats stat : stats) {
			
			Label name = new Label(stat.getName());
			name.setLocation(left + i * width, 310);
			name.setSize(50, 20);
			window.add(name);
			
			Label tower = new Label(stat.getBuiltTowers() + "");
			tower.setLocation(left + i * width, 345);
			tower.setSize(50, 20);
			window.add(tower);
			
			Label bullet = new Label(stat.getTotalBullets() + "");
			bullet.setLocation(left + i * width, 380);
			bullet.setSize(50, 20);
			window.add(bullet);
			
			Label pUnit = new Label(stat.getPassedUnits() + "");
			pUnit.setLocation(left + i * width, 415);
			pUnit.setSize(50, 20);
			window.add(pUnit);
			
			Label point = new Label(stat.getPoints() + "");
			point.setLocation(left + i * width, 450);
			point.setSize(50, 20);
			window.add(point);
			
			i++;
		}
		
		display.add(window);
	}

}
